FROM ubuntu:20.04


# 替换 apt 源为清华源（适用于 Ubuntu 20.04 focal）
RUN sed -i 's/archive.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list && \
    sed -i 's/security.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list

# Install common dependencies
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get -y update && \
    apt-get -y install sudo \ 
    apt-utils \
    build-essential \
    openssl \
    clang \
    graphviz-dev \
    git \
    autoconf \
    libgnutls28-dev \
    libssl-dev \
    llvm \
    python3-pip \
    nano \
    net-tools \
    vim \
    gdb \
    netcat \
    strace \
    wget \
    libcjson-dev \
    cmake

RUN python3 -m pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple --upgrade pip
RUN pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple



# Add a new user ubuntu, pass: ubuntu
RUN groupadd ubuntu && \
    useradd -rm -d /home/ubuntu -s /bin/bash -g ubuntu -G sudo -u 1000 ubuntu -p "$(openssl passwd -1 ubuntu)"

RUN chmod 777 /tmp

RUN pip3 install gcovr==4.2 -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple

# Use ubuntu as default username
USER ubuntu
WORKDIR /home/ubuntu

# Import environment variable to pass as parameter to make (e.g., to make parallel builds with -j)
ARG MAKE_OPT

# Set up fuzzers
RUN git clone https://gitee.com/zhangph12138/aflnet.git && \
    cd aflnet && \
    make clean all $MAKE_OPT && \
    cd llvm_mode && make $MAKE_OPT

RUN git clone https://gitee.com/zhangph12138/aflnwe.git && \
    cd aflnwe && \
    make clean all $MAKE_OPT && \
    cd llvm_mode && make $MAKE_OPT

RUN git clone https://gitee.com/zhangph12138/llmafl.git && \
    cd llmafl && \
    pip install -r requirements.txt -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple &&\
    python3 setup.py build_ext --inplace --verbose




# Set up environment variables for AFLNet
ENV WORKDIR="/home/ubuntu/experiments"
ENV AFLNET="/home/ubuntu/aflnet"
ENV PATH="${PATH}:${AFLNET}:/home/ubuntu/.local/bin:${WORKDIR}"
ENV AFL_PATH="${AFLNET}"
ENV AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES=1 \
    AFL_SKIP_CPUFREQ=1 \
    AFL_NO_AFFINITY=1


RUN mkdir $WORKDIR


COPY --chown=ubuntu:ubuntu rand.patch ${WORKDIR}/rand.patch

# Set up environment variables for ASAN
ENV ASAN_OPTIONS='abort_on_error=1:symbolize=0:detect_leaks=0:detect_stack_use_after_return=1:detect_container_overflow=0:poison_array_cookie=0:malloc_fill_byte=0:max_malloc_fill_size=16777216'

# Download and compile wolfssl for fuzzing
RUN cd ${WORKDIR} && \
    git clone https://gitee.com/zhangph12138/wolfssl.git && \
    cd wolfssl && \
    git checkout 5922b5def5177ad6f1632dd3b961f2336881202c && \
    patch -p1 < ${WORKDIR}/rand.patch && \
    rm -rf build && \
    mkdir -p build && cd build && \
    CC=afl-clang-fast cmake -DWOLFSSL_SESSION_TICKET=yes -DWOLFSSL_FIX_RANDOM_FUZZ=ON .. && \
    AFL_USE_ASAN=1 CC=afl-clang-fast  cmake --build . --target server client

# Download and compile wolfssl for coverage analysis
RUN cd ${WORKDIR} && \
    git clone https://gitee.com/zhangph12138/wolfssl.git wolfssl-gcov && \
    cd wolfssl-gcov && \
    git checkout 5922b5def5177ad6f1632dd3b961f2336881202c && \
    patch -p1 < ${WORKDIR}/rand.patch && \
    mkdir -p build && cd build && \
    CFLAGS="-fprofile-arcs -ftest-coverage" LDFLAGS="-fprofile-arcs -ftest-coverage" cmake .. && \
    CFLAGS="-fprofile-arcs -ftest-coverage" LDFLAGS="-fprofile-arcs -ftest-coverage" cmake --build . --target server client

COPY --chown=ubuntu:ubuntu in-tls ${WORKDIR}/in-tls
COPY --chown=ubuntu:ubuntu tls.dict ${WORKDIR}/tls.dict
COPY --chown=ubuntu:ubuntu certs ${WORKDIR}/certs

  